<?

	class Package_Nicename0v0r00 extends _smuoiPackage{
		
		var $cache = array();
		
		function table(){
			return $this->tables->nicename->name;
		}

		function new_name($nicename, $type='O', $value=''){
			if(($type == 'E')&&(!is_root())) return false;
			list($name, $path, $nicename) = $this->read_nicename($nicename);
			if($this->al()->exists($this->table(), array('name'=>$name, 'path'=>$path))) $this->destroy($nicename);
			$this->create_object($path);
			$id = $this->new_id('n');
			return $this->al()->insert($this->table(), array('id'=>$id, 'type'=>$type, 'name'=>$name, 'path'=>$path, 'value'=>$value)) ? $nicename : false;
		}
		
		private function create_object($path){
			if(!$path) return;
			$a = explode('.', $path);
			$paths = array();
			for($i=0; $i<count($a); $i++) $paths[] = (($i) ? $paths[$i-1].'.' : '').$a[$i];
			foreach($paths as $path){
				$a = explode('.', $path);
				list($name, $path) = array(array_pop($a), implode('.', $a));
				if($this->al()->exists($this->table(), array('name'=>$name, 'path'=>$path, 'type'=>'O'))) continue;
				$this->al()->delete($this->table(), array('name'=>$name, 'path'=>$path));
				$this->al()->insert($this->table(), array('id'=>$this->new_id('n'), 'type'=>'O', 'name'=>$name, 'path'=>$path));
			}
		}
		
		function save_name($nicename, $value){
			if(!$data = $this->get($nicename, false)) return false;
			switch($data->type){
				case 'E':
					if(!is_root()) return false;
				case 'O':
					$value='';
					break;
				case 'Z':
					$value = serialize($value);
					break;
				case 'J':
					$value = json_encode($value);
					break;
				default:
					$value = (string)$value;	
			}
			if(strlen($value) > 255) $data->type = 'B';
			if($data->type == 'B'){
				$blob = $this->db->package('Longtext')->retrieve_my('blob', $data, 1);
				$blob->blob = $value;
				$blob->commit();
				$value = '';
			}
			list($name, $path, $nicename) = $this->read_nicename($nicename);
			return $this->al()->update($this->table(), array('type'=>$data->type, 'value'=>$value), array('id'=>$data->id));
		}
		
		function destroy($nicename){
			list($name, $path, $nicename) = $this->read_nicename($nicename);
			$this->al()->delete($this->table(), array('%path'=>"$nicename%")); 
			$this->al()->delete($this->table(), array('name'=>$name, 'path'=>$path));
		}
		
		function get($nicename, $instantiate=true){
			list($name, $path, $nicename) = $this->read_nicename($nicename);
			if(isset($this->cache[$nicename])) return $this->cache[$nicename];
			if(!$data = $this->al()->select('*', $this->table(), array('name'=>$name, 'path'=>$path))) return NULL;
			return ($instantiate) ? $this->cache[$nicename] = $this->instantiateRow($data[0]) : object($data[0]);											  
		}
		
		function read_nicename($nicename){
			$a = smuoi_explode('.', preg_replace('/^A-Za-z0-9_\./', '', $nicename));
			$nicename = implode('.', $a);
			return array(array_pop($a), implode('.', $a), $nicename);
		}
			
		function instantiateRow($data){
			switch($data['type']){
				case 'L': # Link
					return $this->db->get($data['value']);
				case 'O': # Object
					return new NiceObject01($data, $this);
				case 'B': # Blob
					return $this->db->get($data['value'])->blob();
				case 'E': # Eval
					return eval($data['value']);
				case 'J': # Json
					return new jso(json_decode($data['value']));
				case 'Z': # Seralized
					return unserialize($data['value']);
				case 'S': # String
				case 'I': # Integer
				case 'N': # Number
					return $data['value'];
			}
		}
	}
	
	class NiceObject01{
		
		function __construct($data, $pkg){
			$this->path = $data['path'];
			$this->pkg = $pkg;
		}
		
		function get($k){
			return $this->pkg->get($this->path.'.'.$k);
		}
	}

?>